﻿using System;
using System.Globalization;
using System.Collections.ObjectModel;

namespace NazTek.Extension.Clr4
{
    /// <summary>
    /// ToEnum validations
    /// </summary>
    internal enum ToEnumValidationItem
    {
        /// <summary>
        /// Source is not empty
        /// </summary>
        SourceNotEmpty,
        /// <summary>
        /// Destination is enum type
        /// </summary>
        DestinationIsEnum,
        /// <summary>
        /// Underlying destination type is as expected
        /// </summary>
        ExpectedUnderlyingDesinationType,
        /// <summary>
        /// Source value is defined in destination type
        /// </summary>
        SourceDefinedInDestinaion
    }
    /// <summary>
    /// Utility methods and constants
    /// </summary>
    internal static class Common
    {
        /// <summary>
        /// Qualified byte namespace
        /// </summary>
        internal const string QTN_BYTE = "System.Byte";
        /// <summary>
        /// Qualified signed byte namespace
        /// </summary>
        internal const string QTN_SBYTE = "System.SByte";
        /// <summary>
        /// Qualified short namespace
        /// </summary>
        internal const string QTN_SHORT = "System.Int16";
        /// <summary>
        /// Qualified unsigned short namespace
        /// </summary>
        internal const string QTN_USHORT = "System.UInt16";
        /// <summary>
        /// Qualified int namespace
        /// </summary>
        internal const string QTN_INT = "System.Int32";
        /// <summary>
        /// Qualified unsigned int namespace
        /// </summary>
        internal const string QTN_UINT = "System.UInt32";
        /// <summary>
        /// Qualified long namespace
        /// </summary>
        internal const string QTN_LONG = "System.Int64";
        /// <summary>
        /// Qualified unsigned long namespace
        /// </summary>
        internal const string QTN_ULONG = "System.UInt64";
        /// <summary>
        /// Inserts supplied values into the suppled message template
        /// </summary>
        /// <param name="messageTemplate">Message template</param>
        /// <param name="messageArguments">Arguments to insert into message template</param>
        /// <returns>Formatted message</returns>
        internal static string GetMessage(string messageTemplate, params object[] messageArguments)
        {
            return string.Format(CultureInfo.CurrentCulture, messageTemplate, messageArguments);
        }
        /// <summary>
        /// Generates a random salt byte array
        /// </summary>
        internal static byte[] RandomSalt
        {
            get
            {
                // sleep for 1 ms - prevents a thread from sequentially regenerating the same salt
                System.Threading.Thread.Sleep(1);

                // generate seed
                char[] randomCharacters = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture).ToCharArray();
                int seed = randomCharacters[randomCharacters.Length - 1];

                Random rnd = new Random(seed);

                // generate salt length
                int saltLength = rnd.Next(3, 9);

                // generate salt bytes
                byte[] saltBytes = new byte[saltLength];
                for (int i = 0; i < saltLength; i++)
                    saltBytes[i] = Convert.ToByte(rnd.Next(0, 255));

                return saltBytes;
            }
        }
    }
}
